<?php
if (! defined ( 'BASEPATH' ))
    exit ( 'No direct script access allowed' );
class Merchant_order {
    var $CI;
    var $merchant_id;
    var $notice_url;
    var $order_id;
    var $product_name;
    var $money;
    var $enc_string;
    var $account_no;
    var $pay_type;
    var $item_name;
    var $note;
    var $user_id;
    /**
     *
     * 初始化订单信息
     *
     * @name __construct
     * @author Eifel.Deng 2015年3月23日
     */
    public function __construct() {
        $this->CI = & get_instance ();
        
        $this->set_merchant_id ();
        $this->set_notice_url ();
        $this->set_order_id ();
        $this->set_product_name ();
        $this->set_money ();
        $this->set_encstring ();
        $this->set_account_no ();
        $this->set_device_name ();
        $this->set_pay_type ();
        $this->set_item_name ();
        $this->set_note ();
        $this->set_user_id ();
    }
    
    /**
     * ription: check if the order info is valid.
     *
     * @access public
     * @author hyman
     * @return mixed $infos
     */
    public function check_order_info() {
        if ($this->get_merchant_id () == '') {
            $info ['message'] = "商户信息不完整!";
            $info ['result'] = false;
            return $info;
        }
        if ($this->get_order_id () == '') {
            $info ['message'] = "订单信息不完整!";
            $info ['result'] = false;
            return $info;
        }
        if ($this->get_money () == '') {
            $info ['message'] = "金额不正确!";
            $info ['result'] = false;
            return $info;
        }
        if ($this->get_encstring () == '') {
            $info ['message'] = "商户验证信息不完整!";
            $info ['result'] = false;
            return $info;
        }
        $merchant_key = $this->get_mechant_key ();
        if ($merchant_key == null) {
            $info ['message'] = "商户信息不存在!";
            $info ['result'] = false;
            return $info;
        }
        $enc_string = $this->enc_order_string ();
        if ($enc_string != $this->get_encstring ()) {
            $info ['message'] = "支付信息验证失败!";
            $info ['result'] = false;
            return $info;
        }
        $info ['message'] = "支付信息完整!";
        $info ['result'] = true;
        return $info;
    }
    
    /**
     * ription: check if order is submit? if not ,make order.!.
     *
     * @access public
     * @author hyman
     * @return mixed $infos
     */
    public function make_order() {
        $merchant_order = $this->get_merchant_order_info ();
        if ($merchant_order != null) {
            if ($merchant_order ['PAY_STATUS'] != 0) {
                $info ['message'] = "订单已经支付过了!";
                $info ['result'] = false;
                return $info;
            } else {
                // 更新现有的订单信息
                $this->update_new_order_info ( $merchant_order ['TRAN_ID'] );
                $info ['tran_id'] = $merchant_order ['TRAN_ID'];
                $info ['history'] = true;
                $info ['result'] = true;
                return $info;
            }
        } else {
            $info ['tran_id'] = $this->insert_order_transcation ();
            $info ['history'] = false;
            $info ['result'] = true;
            return $info;
        }
    }
    
    /**
     * ription: get merchant key from database by using merchant id.
     *
     * @access private
     * @author hyman
     * @return varchar $merchant_key
     */
    private function get_mechant_key() {
        $this->CI->load->model ( "mpay_merchant_model" );
        return $this->CI->mpay_merchant_model->query_merchant_key ( $this->get_merchant_id () );
    }
    
    /**
     * ription: encode order infos by using md5 function.
     *
     * @access private
     * @author hyman
     * @return varchar $enc_string
     */
    private function enc_order_string() {
        return md5 ( $this->get_merchant_id () . $this->get_notice_url () . $this->get_order_id () . $this->get_mechant_key () );
    }
    
    /**
     * ription: get merchant order infos from database by using merchant id and order id.
     *
     * @access private
     * @author hyman
     * @return varchar $merchant_order_info
     */
    private function get_merchant_order_info() {
        $this->CI->load->model ( "mpay_transaction_model" );
        return $this->CI->mpay_transaction_model->get_merchant_order_info ( $this->get_merchant_id (), $this->get_order_id () );
    }
    
    /**
     * ription: insert transcation infos into database by using model.
     *
     * @access public
     * @author hyman
     * @param mixed $merchant_order_infos            
     * @return int $tran_id
     */
    private function insert_order_transcation() {
        $this->CI->load->model ( "mpay_transaction_model" );
        $merchant_order_info ['pay_status'] = 0;
        $merchant_order_info ['return_status'] = 0;
        $merchant_order_info ['created_by'] = "merchant_proxy";
        $merchant_order_info ['created_time'] = date ( "Y-m-d H:i:s" );
        $merchant_order_info ['tran_type'] = 'normal';
        
        $merchant_order_info ['merchant_id'] = $this->get_merchant_id ();
        $merchant_order_info ['notice_url'] = $this->get_notice_url ();
        $merchant_order_info ['back_url'] = "";
        $merchant_order_info ['money'] = $this->get_money ();
        $merchant_order_info ['order_id'] = $this->get_order_id ();
        $merchant_order_info ['pay_type'] = $this->get_pay_type ();
        $merchant_order_info ['product_name'] = $this->get_product_name ();
        $merchant_order_info ['user_id'] = $this->get_user_id ();
        $merchant_order_info ['note'] = $this->get_note ();
        if ($merchant_order_info ['product_name'] == "") {
            $merchant_order_info ['product_name'] = $this->get_item_name ();
        }
        
        return $this->CI->mpay_transaction_model->insert_transcation ( $merchant_order_info );
    }
    /**
     * 更新新的订单信息
     */
    public function update_new_order_info($tran_id) {
        $this->CI->load->model ( "mpay_transaction_model" );
        $this->CI->mpay_transaction_model->update_new_order_info ( $tran_id, $this->get_money () );
    }
    
    /**
     * ription: query transaction informations from database by $tran_id
     *
     * @access public
     * @author hyman
     * @param int $tran_id            
     * @return mixed $transaction_info
     */
    public function get_transaction_info($tran_id) {
        $this->CI->load->model ( "mpay_transaction_model" );
        return $this->CI->mpay_transaction_model->get_transaction_info ( $tran_id );
    }
    
    /**
     * ription: update the order payer.
     *
     * @access public
     * @author hyman
     * @param int $tran_id            
     * @return null
     */
    public function update_order_payer($tran_id, $account_no) {
        $this->CI->load->model ( "mpay_transaction_model" );
        $this->CI->mpay_transaction_model->update_order_payer ( $tran_id, $account_no );
    }
    
    /**
     * ription:set merchant_id.
     *
     * @access private
     * @author hyman
     * @param
     *            null
     * @return null
     */
    private function set_merchant_id() {
        $this->merchant_id = $this->CI->input->get_post ( "MerId" );
    }
    
    /**
     * ription:get merchant_id.
     *
     * @access public
     * @author hyman
     * @param
     *            null
     * @return $merchant_id
     */
    public function get_merchant_id() {
        return $this->merchant_id;
    }
    private function set_notice_url() {
        $this->notice_url = $this->CI->input->get_post ( "NoticeUrl" );
    }
    public function get_notice_url() {
        return $this->notice_url;
    }
    private function set_order_id() {
        $this->order_id = $this->CI->input->get_post ( "OrderId" );
    }
    public function get_order_id() {
        return $this->order_id;
    }
    private function set_product_name() {
        $this->product_name = $this->CI->input->get_post ( "ProdName" );
    }
    public function get_product_name() {
        return $this->product_name;
    }
    private function set_money() {
        $this->money = $this->CI->input->get_post ( "Price" );
    }
    public function get_money() {
        return $this->money;
    }
    private function set_encstring() {
        $this->enc_string = $this->CI->input->get_post ( "EncString" );
    }
    public function get_encstring() {
        return $this->enc_string;
    }
    private function set_account_no() {
        $this->account_no = $this->CI->input->get_post ( "AccountNo" );
    }
    public function get_account_no() {
        return $this->account_no;
    }
    private function set_device_name() {
        $this->device_name = $this->CI->input->get_post ( "DeviceName" );
    }
    public function get_device_name() {
        return $this->device_name;
    }
    private function set_pay_type() {
        $this->pay_type = $this->CI->input->get_post ( "PayType" );
    }
    public function get_pay_type() {
        return $this->pay_type;
    }
    private function set_item_name() {
        $this->item_name = $this->CI->input->get_post ( "ItemName" );
    }
    public function get_item_name() {
        return $this->item_name;
    }
    private function set_note() {
        $this->note = $this->CI->input->get_post ( "Note" );
    }
    public function get_note() {
        return $this->note;
    }
    private function set_user_id() {
        $this->user_id = $this->CI->input->get_post ( "UserId" );
    }
    public function get_user_id() {
        return $this->user_id;
    }
}
